//
//  MenuView.m
//  BaiTuan
//
//  Created by 看着太阳吃烧饼 on 15/12/16.
//  Copyright © 2015年 YR. All rights reserved.
//

#import "MenuView.h"

@implementation MenuView
{
    NSMutableArray *_subNameArray;
    NSMutableArray *_subIDArray;
    NSNumber *_subID;
    NSMutableDictionary *_catDictionary;
    
    NSMutableArray *_data1;
    NSMutableArray *_data2;
    NSMutableArray *_data3;
    
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
    NSInteger _currentData3Index;
    
    NSInteger _currentData1SelectedIndex;
    
    NSArray *_food;
}




- (instancetype)initWithOrigin:(CGPoint)origin andHeight:(CGFloat)height
{
    self = [super initWithOrigin:origin andHeight:height];
    if (self) {
        
        // 指定默认选中
        _currentData1Index = 0;
        _currentData1SelectedIndex = 0;
        
//        NSArray *food = @[@"全部美食", @"火锅", @"川菜", @"西餐", @"自助餐"];
//        NSArray *travel = @[@"全部旅游", @"周边游", @"景点门票", @"国内游", @"境外游"];
//        
        [self loadDealClassifyData];
//        _data1 = [NSMutableArray arrayWithObjects:@{@"title":@"美食", @"data":food}, @{@"title":@"旅游", @"data":travel}, nil];
        _data2 = [NSMutableArray arrayWithObjects:@"智能排序", @"离我最近", @"评价最高", @"最新发布", @"人气最高", @"价格最低", @"价格最高", nil];
        _data3 = [NSMutableArray arrayWithObjects:@"不限人数", @"单人餐", @"双人餐", @"3~4人餐", nil];
        
        
        //箭头颜色
        self.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
        //按钮分隔线颜色
        self.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
        
        self.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];

        self.dataSource = self;
        self.delegate = self;
    }
    return self;
}

//menu列数
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 3;
}


//第三列显示为collectionView，其余tableView
-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
    if (column==2) {
        
        return YES;
    }
    
    return NO;
}

//下拉菜单显示左右模式
-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    if (column==0) {
        return YES;
    }
    return NO;
}

//下拉菜单右侧的比例（0-1）
-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    if (column==0) {
        return 0.3;
    }
    
    return 1;
}


-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    if (column==0) {
        
        return _currentData1Index;
        
    }
    if (column==1) {
        
        return _currentData2Index;
    }
    
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column==0) {
        if (leftOrRight==0) {
            
            return _data1.count;
        } else{
            
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] count];
        }
    } else if (column==1){
        
        return _data2.count;
        
    } else if (column==2){
        
        return _data3.count;
    }
    
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
        case 0: return [[_data1[_currentData1Index] objectForKey:@"data"] objectAtIndex:_currentData1SelectedIndex];
            break;
        case 1: return _data2[_currentData2Index];
            break;
        case 2: return _data3[_currentData3Index];
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        if (indexPath.leftOrRight==0) {
            NSDictionary *menuDic = [_data1 objectAtIndex:indexPath.row];
            return [menuDic objectForKey:@"title"];
        } else{
            NSInteger leftRow = indexPath.leftRow;
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] objectAtIndex:indexPath.row];
        }
    } else if (indexPath.column==1) {
        
        return _data2[indexPath.row];
        
    } else {
        
        return _data3[indexPath.row];
    }
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0) {
        
        if(indexPath.leftOrRight==0){
            
            _currentData1Index = indexPath.row;
            
            
            return;
        }
        else
        {
            NSDictionary *menuDic = [_data1 objectAtIndex:_currentData1Index];
            NSArray *array = [menuDic objectForKey:@"id"];
            _subID = array[indexPath.row];
            
            NSDictionary *dic = @{
                                  @"subID" : _subID
                                  };
            [[NSNotificationCenter defaultCenter]postNotificationName:kClassifyChangeNotification object:nil userInfo:dic];
        }
        
    } else if(indexPath.column == 1){
        
        _currentData2Index = indexPath.row;
        
    } else{
        
        _currentData3Index = indexPath.row;
    }
}

- (void)loadDealClassifyData
{
    NSString *classifyPath = [[NSBundle mainBundle] pathForResource:@"dealClassify" ofType:@"json"];
    
    NSData *data = [NSData dataWithContentsOfFile:classifyPath];
    
    NSDictionary *classifyDic = [NSJSONSerialization JSONObjectWithData:data
                                                                options:NSJSONReadingMutableContainers error:nil];
    NSArray *categoriesArray = classifyDic[@"categories"];
    _data1 = [[NSMutableArray alloc]init];
    for (NSDictionary *catDic in categoriesArray) {
        
        NSArray *subcategoriesArray = catDic[@"subcategories"];
        _subNameArray = [[NSMutableArray alloc]init];
        _subIDArray = [[NSMutableArray alloc]init];
        for (NSDictionary *subDic in subcategoriesArray) {
            
            [_subNameArray addObject:subDic[@"subcat_name"]];
            [_subIDArray addObject:subDic[@"subcat_id"]];
            
        }
        _catDictionary = [[NSMutableDictionary alloc]init];
        
        [_catDictionary setObject:catDic[@"cat_name"] forKey:@"title"];
        [_catDictionary setObject:_subNameArray forKey:@"data"];
        [_catDictionary setObject:_subIDArray forKey:@"id"];
        [_data1 addObject:_catDictionary];
    }
    
}
@end
